package com.ruoyi.system.service.impl;


import com.ruoyi.system.domain.Dynamic;
import com.ruoyi.system.mapper.DynamicMapper;
import com.ruoyi.system.mapper.TalkMapper;
import com.ruoyi.system.service.DynamicService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DynamicServiceImpl implements DynamicService {


    @Autowired
    private DynamicMapper dynamicDao;

    @Autowired
    private TalkMapper talkDao;

    private List<Dynamic> tempReplyPlus = new ArrayList<>();

    private List<Dynamic> tempReplyPlusS = new ArrayList<>();

    @Override
    public int saveDynamic(Dynamic dynamic) {
        dynamic.setCreateTime(new Date());
         dynamicDao.saveDynamic(dynamic);
        int commentCountById = talkDao.getCommentCountById(dynamic.getDynamicId());
        return commentCountById;
    }
    @Override
    public List<Dynamic> findByParentId( Long parentCommentId){
        return dynamicDao.findByParentId(parentCommentId);
    }
    @Override
    public void deleteDynamic(Dynamic dynamic, Long id) {
        List<Dynamic> byParentId = dynamicDao.findByParentId(id);
        dynamicDao.deleteDynamic(id);
        if(!byParentId.isEmpty()){
            for(Dynamic d : byParentId){
                dynamicDao.deleteDynamic(d.getId());
                if(d.getParentCommentId()!=d.getId()){
                    List<Dynamic> byParentId1 = dynamicDao.findByParentId(d.getParentCommentId());
                    for(Dynamic dd : byParentId1){
                        dynamicDao.deleteDynamic(dd.getId());
                    }
                }
            }
        }
        talkDao.getCommentCountById(dynamic.getDynamicId());
    }

    @Override
    public List<Dynamic> listDynamicByDynamicId(Long dynamicId) {
        List<Dynamic> dynamics = dynamicDao.findByDynamicIdParentIdNull(dynamicId, Long.parseLong("-1"));
        for (Dynamic dynamic : dynamics){
            Long id = dynamic.getId();
            String nickname = dynamic.getNickname();
            List<Dynamic> childDynamics = dynamicDao.findByDynamicIdParentIdNotNull(dynamicId, id);
            combineChildren(dynamicId,childDynamics,nickname);
            dynamic.setReplyDynamics(tempReplyPlus);
            tempReplyPlus = new ArrayList<>();
        }
        return dynamics;
    }

    private void combineChildren(Long dynamicId, List<Dynamic> childComments, String parentNickname1) {
        //判断是否有一级子评论
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Dynamic childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplyPlus.add(childComment);
                Long childId = childComment.getId();
                //查询出子二级评论
                recursively(dynamicId, childId, parentNickname);
            }
        }
    }

    private void recursively(Long dynamicId, Long childId, String parentNickname1) {
        //根据子一级评论的id找到子二级评论
        List<Dynamic> replayDynamics = dynamicDao.findByDynamicIdAndReplayId(dynamicId,childId);

        if(replayDynamics.size() > 0){
            for(Dynamic replayDynamic : replayDynamics){
                String parentNickname = replayDynamic.getNickname();
                replayDynamic.setParentNickname(parentNickname1);
                Long replayId = replayDynamic.getId();
                tempReplyPlus.add(replayDynamic);
                recursively(dynamicId,replayId,parentNickname);
            }
        }
    }





    /*========================================================*/




    @Override
    public List<Dynamic> getDynamicByUserId(Long userId) {
        List<Dynamic> dynamics = dynamicDao.findByUserIdParentIdNull(userId, Long.parseLong("-1"));
        for (Dynamic dynamic : dynamics){
            Long id = dynamic.getId();
            String nickname = dynamic.getNickname();
            List<Dynamic> dynamicList = dynamicDao.findByUserIdParentIdNotNull(userId, id);
            combineChildrenS(userId,dynamicList,nickname);
            dynamic.setReplyDynamics(tempReplyPlusS);
            tempReplyPlusS = new ArrayList<>();
        }
        return dynamics;
    }

    private void combineChildrenS(Long userId,List<Dynamic> childDynamics,String nickname){
        if(childDynamics.size()>0){
            for (Dynamic childDynamic : childDynamics){
                String parentNickname = childDynamic.getNickname();
                childDynamic.setParentNickname(nickname);
                tempReplyPlusS.add(childDynamic);
                Long childId = childDynamic.getId();
                recursivelyS(userId,childId,parentNickname);
            }
        }
    }

    private void recursivelyS(Long userId,Long childId,String parentNickname){
        List<Dynamic> replayDynamics = dynamicDao.findByUserIdAndReplayId(userId, childId);
        if(replayDynamics.size() > 0){
            for(Dynamic replayDynamic : replayDynamics){
                String nickname = replayDynamic.getNickname();
                replayDynamic.setParentNickname(parentNickname);
                Long replayId = replayDynamic.getId();
                tempReplyPlusS.add(replayDynamic);
                recursivelyS(userId,replayId,nickname);
            }
        }
    }

}
